package cn.tedu.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Application Lifecycle Listener implementation class OnLineListener
 *
 */
public class OnLineListener implements HttpSessionListener {
	private int count=0;
    /**
     * Default constructor. 
     */
    public OnLineListener() {
        System.out.println("统计在线人数的监听器的生命周期开始");
    }

	/**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent se)  { 
    	System.out.println("有一个session对象在服务端创建了");
    	count++;
    	HttpSession session=se.getSession();
    	ServletContext application=session.getServletContext();
    	application.setAttribute("onLine", count);
    }

	/**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent se)  { 
    	System.out.println("有一个session对象在服务端销毁了");
    	count--;
    	HttpSession session=se.getSession();
    	ServletContext application=session.getServletContext();
    	application.setAttribute("onLine", count);
    }
	
}
